﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace tmsWeb
{
    public partial class SearchBase :  micsun.Web.SecurityPage
    {
        private DataTable _SchemaTable;
        private DataTable _SchemaTable1;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                DGLoad();
                initSearchBar();
            }
        }

        private void DGLoad()
        {
            ResultDG.SelectList = "SiteID,SiteName";
            ResultDG.TableList = "t_site_tree";
            ResultDG.KeyIDName = "KeyId";
            ResultDG.WhereList = "LayID ='1'";
            ResultDG.KeyIDPosition = new int[] { 1 };
            ResultDG.BindData(BindDataType.搜索位置);
            _SchemaTable = ResultDG.SchemaTable;

            ResultDG1.SelectList = "CltKey,CltName,CltSerial,CltState,tsn,RptNumber,SsrNumber";//select CltKey,CltName,CltSerial,CltState,tsn,RptNumber,SsrNumber from t_collections
            ResultDG1.TableList = "t_collections";
            ResultDG1.KeyIDName = "KeyId";
            ResultDG1.KeyIDPosition = new int[] { 1 };
            ResultDG1.BindData(BindDataType.搜索位置);
            _SchemaTable1 = ResultDG1.SchemaTable;
            initSearchBar1();
            
        }
        void initSearchBar()
        {
            SelectList.Items.Clear();
            if (_SchemaTable == null) return;
            //_SchemaTable.Rows[0].Delete();
            _SchemaTable.AcceptChanges();
            _SchemaTable.Columns.Add("locdesc");

            for (int i = 0; i < ResultDG.Columns.Count; i++)
            {
                string opField = string.Empty;
                try
                {
                    opField = ((BoundColumn)ResultDG.Columns[i]).DataField;
                }
                catch
                {
                    continue;
                }
                if (ResultDG.Columns[i].Visible == true)
                {
                    DataRow[] foundRows = _SchemaTable.Select("ColumnName='" + opField + "'");
                    ListItem li = new ListItem(ResultDG.Columns[i].HeaderText, foundRows[0][0].ToString() + "|" + foundRows[0][11].ToString());
                    SelectList.Items.Add(li);
                }
            }
            txtContent.Attributes.Add("onkeydown", "if(event.keyCode==13){" + SelectBtn.UniqueID + ".click();return false;}");
        }
        void initSearchBar1()
        {
            SelectList1.Items.Clear();
            if (_SchemaTable1 == null) return;
            //_SchemaTable.Rows[0].Delete();
            _SchemaTable1.AcceptChanges();
            _SchemaTable1.Columns.Add("locdesc");

            for (int i = 0; i < ResultDG1.Columns.Count; i++)
            {
                string opField = string.Empty;
                try
                {
                    opField = ((BoundColumn)ResultDG1.Columns[i]).DataField;
                }
                catch
                {
                    continue;
                }
                if (ResultDG1.Columns[i].Visible == true)
                {
                    DataRow[] foundRows = _SchemaTable1.Select("ColumnName='" + opField + "'");
                    ListItem li = new ListItem(ResultDG1.Columns[i].HeaderText, foundRows[0][0].ToString() + "|" + foundRows[0][11].ToString());
                    SelectList1.Items.Add(li);
                }
            }
            txtContent1.Attributes.Add("onkeydown", "if(event.keyCode==13){" + SelectBtn1.UniqueID + ".click();return false;}");
        }
        void initSearchBar2()
        {
            SelectList1.Items.Clear();
            if (_SchemaTable1 == null) return;
            //_SchemaTable.Rows[0].Delete();
            _SchemaTable1.AcceptChanges();
            _SchemaTable1.Columns.Add("locdesc");

            for (int i = 0; i < GridViewPlus1.Columns.Count; i++)
            {
                string opField = string.Empty;
                try
                {
                    opField = ((BoundColumn)GridViewPlus1.Columns[i]).DataField;
                }
                catch
                {
                    continue;
                }
                if (GridViewPlus1.Columns[i].Visible == true)
                {
                    DataRow[] foundRows = _SchemaTable1.Select("ColumnName='" + opField + "'");
                    ListItem li = new ListItem(GridViewPlus1.Columns[i].HeaderText, foundRows[0][0].ToString() + "|" + foundRows[0][11].ToString());
                    SelectList1.Items.Add(li);
                }
            }
            txtContent1.Attributes.Add("onkeydown", "if(event.keyCode==13){" + SelectBtn1.UniqueID + ".click();return false;}");
        }
        void initSearchBar3()
        {
            SelectList1.Items.Clear();
            if (_SchemaTable1 == null) return;
            //_SchemaTable.Rows[0].Delete();
            _SchemaTable1.AcceptChanges();
            _SchemaTable1.Columns.Add("locdesc");

            for (int i = 0; i < GridViewPlus2.Columns.Count; i++)
            {
                string opField = string.Empty;
                try
                {
                    opField = ((BoundColumn)GridViewPlus2.Columns[i]).DataField;
                }
                catch
                {
                    continue;
                }
                if (GridViewPlus2.Columns[i].Visible == true)
                {
                    DataRow[] foundRows = _SchemaTable1.Select("ColumnName='" + opField + "'");
                    ListItem li = new ListItem(GridViewPlus2.Columns[i].HeaderText, foundRows[0][0].ToString() + "|" + foundRows[0][11].ToString());
                    SelectList1.Items.Add(li);
                }
            }
            txtContent1.Attributes.Add("onkeydown", "if(event.keyCode==13){" + SelectBtn1.UniqueID + ".click();return false;}");
        }

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            if (e.Item.Value == "0")
            {
                MultiView1.ActiveViewIndex = 0;
            }
            else {
                MultiView1.ActiveViewIndex = 1;
 
            }
        }

        protected void SelectBtn_Click(object sender, EventArgs e)
        {
            if (((Button)sender).ID.ToLower() == "selectbtn")
            {
                string[] SearchFields = Request.Params[SelectList.UniqueID].Split('|');
                string searchField = SearchFields[0];
                string datatype = SearchFields[1];
                string oprater = Request.Params[SelectLogic.UniqueID];
                string searchvalue = Request.Params[txtContent.UniqueID];
                int stype = 1;
                if (Request.Params[TwoCbx.UniqueID] != null) stype = 2;
                string messagse = ResultDG.AddSearchCond(searchField, oprater, searchvalue, datatype, (SearchType)stype);
                if (messagse != null && messagse != string.Empty)
                    AddMessage(messagse);
                ResultDG.BindData(BindDataType.分页);
            }
            else
            {
                string[] SearchFields = Request.Params[SelectList1.UniqueID].Split('|');
                string searchField = SearchFields[0];
                string datatype = SearchFields[1];
                string oprater = Request.Params[SelectLogic1.UniqueID];
                string searchvalue = Request.Params[txtContent1.UniqueID];
                int stype = 1;
                if (Request.Params[TwoCbx1.UniqueID] != null) stype = 2;
                string messagse = string.Empty;
                
                switch (MultiView2.ActiveViewIndex)
                {
                    case 1:
                        messagse = GridViewPlus1.AddSearchCond(searchField, oprater, searchvalue, datatype, (SearchType)stype);
                        GridViewPlus1.BindData(BindDataType.分页);
                        break;
                    case 2:
                        messagse = GridViewPlus2.AddSearchCond(searchField, oprater, searchvalue, datatype, (SearchType)stype);
                        GridViewPlus2.BindData(BindDataType.分页);
                        break;
                    default:
                        messagse = ResultDG1.AddSearchCond(searchField, oprater, searchvalue, datatype, (SearchType)stype);
                        ResultDG1.BindData(BindDataType.分页);
                        break;
                }
                if (messagse != null && messagse != string.Empty)
                    AddMessage(messagse);
            }
        }

        protected void SelectType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResultDG.SelectList = "SiteID,SiteName";
                ResultDG.TableList = "t_site_tree";
                ResultDG.KeyIDName = "KeyId";
            if (SelectType.SelectedValue == "0")
            {
                ResultDG.WhereList = "LayID ='1'";
            }
            else
            {
                ResultDG.WhereList = "LayID ='2'";
            }
            ResultDG.KeyIDPosition = new int[] { 1 };
            ResultDG.BindData(BindDataType.搜索位置);
            _SchemaTable = ResultDG.SchemaTable;
            initSearchBar();
        }

        protected void SelectType1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MultiView2.ActiveViewIndex = SelectType1.SelectedIndex;

            if (SelectType1.SelectedIndex == 0)
            {
                ResultDG1.SelectList = "CltKey,CltName,CltSerial,CltState,tsn,RptNumber,SsrNumber";//select CltKey,CltName,CltSerial,CltState,tsn,RptNumber,SsrNumber from t_collections
                ResultDG1.TableList = "t_collections";
                ResultDG1.KeyIDName = "KeyId";
                ResultDG1.KeyIDPosition = new int[] { 1 };
                ResultDG1.BindData(BindDataType.搜索位置);
                _SchemaTable1 = ResultDG1.SchemaTable;
                initSearchBar1();
            }
            else if (SelectType1.SelectedIndex == 1)
            {
                GridViewPlus1.SelectList = "RptKey,RptName,CltKey,RptState,SsrNumber";
                GridViewPlus1.TableList = "t_repeaters";
                GridViewPlus1.KeyIDName = "KeyId";
                GridViewPlus1.KeyIDPosition = new int[] { 1 };
                GridViewPlus1.BindData(BindDataType.搜索位置);
                _SchemaTable1 = GridViewPlus1.SchemaTable;
                initSearchBar2();
            }
            else if (SelectType1.SelectedIndex == 2)
            {
                GridViewPlus2.SelectList = "SsrKey,SsrName,STPID,SsrState,cbt,Temperature";
                GridViewPlus2.TableList = "t_sensors";
                GridViewPlus2.KeyIDName = "KeyId";
                GridViewPlus2.KeyIDPosition = new int[] { 1 };
                GridViewPlus2.BindData(BindDataType.搜索位置);
                _SchemaTable1 = GridViewPlus2.SchemaTable;
                initSearchBar3();
            }
        }
    }
}
